/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Product;
import model.ProductService;
import model.SaleOrder;
import model.SaleOrderDetail;
import model.SaleOrderDetailService;
import model.SaleOrderService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author SONY
 */
public class UpdateOrderStrutsAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String error_SaleOrderID = "";
        try {
            String SaleOrderID_str = request.getParameter("SaleOrderID");
            String Status = request.getParameter("Status");
            String btnSubmit = request.getParameter("btnSubmit");
            if(SaleOrderID_str.length() == 0) {
                error_SaleOrderID = "Mã Đơn Hàng không được bỏ trống.";
            } else {
                int SaleOrderID = Integer.parseInt(SaleOrderID_str);
                SaleOrderService sale_order_service = new SaleOrderService();
                ArrayList<SaleOrder> lstSaleOrders = sale_order_service.getSaleOrders_bySaleOrderID(SaleOrderID);
                String error_lstSaleOrder = null;
                if(lstSaleOrders.isEmpty()) {
                    error_lstSaleOrder = "Mã Đơn Hàng này không được tìm thấy. Hãy kiểm tra lại!";
                } else {
                    String error_update_status = "";
                    if(btnSubmit.equals("Cập Nhật")) {
                        for(int i = 0; i < lstSaleOrders.size(); i++) {
                            if(lstSaleOrders.get(i).getStatus().equals("Hủy")) {
                                error_update_status = "Không thể cập nhật trạng thái đơn hàng cho những đơn hàng đã bị hủy.";
                            } else {
                                if(sale_order_service.changeStatus(SaleOrderID, Status)) {
                                    lstSaleOrders = sale_order_service.getSaleOrders_bySaleOrderID(SaleOrderID);
                                    if(Status.equals("Hủy")) {
                                        SaleOrderDetailService sale_order_detail_service = new SaleOrderDetailService();
                                        ArrayList<SaleOrderDetail> lstSaleOrderDetails = sale_order_detail_service.getSaleOrderDetails_bySaleOrderID(SaleOrderID);
                                        for(int j = 0; j < lstSaleOrderDetails.size(); j++) {
                                            String ProductID = lstSaleOrderDetails.get(j).getProductID();
                                            int OrderQty = lstSaleOrderDetails.get(j).getOrderQuantity();
                                            //cập nhật lại sl hàng tồn kho khi trạng thái đặt hàng bị hủy bỏ
                                            ProductService product_service = new ProductService();
                                            Product product = product_service.getProductDetails_byPID(ProductID);
                                            int QOH = product.getQOH();
                                            int new_QOH = QOH + OrderQty;
                                            if(product_service.changeQOH(ProductID, new_QOH)) {
                                                System.out.println("inserted successful!");
                                            }
                                        }
                                    }
                                    System.out.println("changed Status successful!");
                                }
                            }
                        }
                    }
                    request.setAttribute("error_update_status", error_update_status);
                }
                request.setAttribute("lstSaleOrders", lstSaleOrders);
                request.setAttribute("SaleOrderID_str", SaleOrderID_str);
                request.setAttribute("error_lstSaleOrder", error_lstSaleOrder);
                return mapping.findForward(SUCCESS);
            }            
        } catch(Exception e) {
            System.err.println(e);
        }
        request.setAttribute("error_SaleOrderID", error_SaleOrderID);
        return mapping.findForward("error");
    }
}
